package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    JedisUtil jedisUtil;

    @Override
    public void add(Setmeal setmeal) {
        //保存套餐基本信息并返回主键
        setmealMapper.add(setmeal);
        //批量建立关系
        Integer setmealId = setmeal.getId();
        List<Integer> checkgroupIds = setmeal.getCheckgroupIds();
        if (CollectionUtil.isNotEmpty(checkgroupIds)) {
            setmealMapper.setSetmealAndCheckGroupBatch(setmealId, checkgroupIds);
        }
        List<Setmeal> setmeals = setmealMapper.getSetmeal();
        String toJSONString = JSON.toJSONString(setmeals);
        jedisUtil.set("setmealJsonceshi",toJSONString);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        setmealMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Setmeal> getSetmeal() {
        String setmealJson = jedisUtil.get("setmealJsonceshi");
        if (null==setmealJson || setmealJson.length()==0){
            List<Setmeal> setmeals = setmealMapper.getSetmeal();
            String JsonSetmeals = JSON.toJSONString(setmeals);
            jedisUtil.set("setmealJsonceshi",JsonSetmeals);
        }
        List<Setmeal> setmeals = (List<Setmeal>) JSON.parse(setmealJson);
        return setmeals;
    }
    @Override
    public Setmeal findById(Integer id) {
        //要提高系統查詢性能,可以將所有的套餐詳情都查詢出來，丟入redis中，再根据id查询对应的套餐详情
        //根据套餐id查询套餐详情

        String setmeal = jedisUtil.hget("setmeal", String.valueOf(id));
        Setmeal setmealJson = JSON.parseObject(setmeal, Setmeal.class);
        if (null == setmeal) {
//            Setmeal setmeal1 = setmealMapper.findById(id);
            setmealJson = findByIdBatch(id);
            String jsonString = JSON.toJSONString(setmealJson);
            jedisUtil.hset("setmeal", String.valueOf(id),jsonString);
            //根据套餐id查询套餐下面所有的检查组
            /*if (CollectionUtil.isNotEmpty(checkGroups)) {
                //循环检查组查询下面的检查项
                for (CheckGroup checkGroup : checkGroups) {
                    List<CheckItem> checkItems = setmealMapper.findCheckItemByCheckGroupId(checkGroup.getId());
                    checkGroup.setCheckItems(checkItems);
                }
            }*/
        }

        return setmealJson;
    }
    @Override
    public Setmeal findByIdBatch(Integer id) {
        //        //根据套餐id查询套餐详情
        Setmeal setmeal = setmealMapper.findById(id);
        if (null != setmeal) {
            //根据套餐id查询套餐下面所有的检查组
            List<CheckGroup> checkGroups = setmealMapper.findCheckGroupBySetmealId(id);
            if (CollectionUtil.isNotEmpty(checkGroups)) {
                List<Integer> ids = new ArrayList<>();
                //循环检查组查询下面的检查项
                for (CheckGroup checkGroup : checkGroups) {
                    ids.add(checkGroup.getId());
                }
                List<CheckItem> checkItems = setmealMapper.findCheckItemByCheckGroupIdBatch(ids);
                //手动分组
                Map<Integer, List<CheckItem>> map = new HashMap<>();

                map = checkItems.stream().collect(Collectors.groupingBy(CheckItem::getCheckGroupId));

                for (CheckGroup checkGroup : checkGroups) {
                    checkGroup.setCheckItems(map.get(checkGroup.getId()));
                }
            }
            setmeal.setCheckGroups(checkGroups);
        }
        return setmeal;
    }


}
